package com.weibo.click;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Timer;

import javax.imageio.ImageIO;

public class ControlUtils {
    private static Robot robot;
    
	private String fileName;// 文件的前缀
	private String defaultName = "GuiCamera";
	static int serialNum = 0;
	private String imageFormat;// 图像文件的格式
	private String defaultImageFormat = "png";
	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

	private static final String TASKLIST = "tasklist";
	private static final String KILL = "taskKill /F /IM firefox.exe";
	private static final String START = "\"C:\\Program Files\\Mozilla Firefox\\firefox.exe\"   \"http://t.qq.com\"";
	// 9v0q8r4y.default - chinku 
	//08l2frni.default  - 秀美VM
	private static final String ffProfile1="\"C:/Documents and Settings/Administrator/Application Data/Mozilla/Firefox/Profiles/08l2frni.default/gm_scripts/Wei_Bo/\"";
	private static final String ffProfile2="\"C:/Documents and Settings/Administrator/Application Data/Mozilla/Firefox/Profiles/08l2frni.default/gm_scripts/Wei_Bo_Login/\"";
	private static final String FFLogout="cmd /c start c:\\startUpFF.bat";
	private static final String  msgFile="http://90.198.120.60:8080/WebTruth/scripts/msg.js";
	
	static{
		try {
			robot=new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		ControlUtils.FFLogoutScript();
	}
	public static void downloadPublicJS() throws IOException{
		robot.delay(800);
		Runtime.getRuntime().exec("curl -o "+ffProfile1+"msg.js"+" "+msgFile );
		robot.delay(5800);
		Runtime.getRuntime().exec("curl -o "+ffProfile2+"msg.js"+" "+msgFile );
		robot.delay(5800);
	}
	//private static final String START = "start  firefox.exe -new-window http://t.qq.com";
	public static int genRandomSeconds(int start,int increment){ 
		Random random = new Random(System.currentTimeMillis());
		int k = random.nextInt();
		int sec = Math.abs(k % start) + increment;
		return sec;
	}
	
	public static void mouseMoveMock(Timer timer){
		int mockMoveTimes=ControlUtils.genRandomSeconds(5,5);
		System.out.println("mockMoveTimes:= "+mockMoveTimes);  
		for(int i=0;i<mockMoveTimes;i++){
			int wheelAmt=ControlUtils.genRandomSeconds(2,-1);//鼠标滚轮随机上下滚动2次
			robot.mouseWheel(wheelAmt);
			robot.delay(40);//随机休息
			int rest1=ControlUtils.genRandomSeconds(600,1200);
			int wheelAmtNega=(-1)*wheelAmt;//鼠标滚轮随机上下滚动2次
			robot.mouseWheel(wheelAmtNega);
			//模拟鼠标随机移动5次
			Point mousepoint = MouseInfo.getPointerInfo().getLocation();   
			int randomXPos=ControlUtils.genRandomSeconds(30,-15);
			int randomYPos=ControlUtils.genRandomSeconds(24,-12);
			robot.mouseMove(mousepoint.x+randomXPos,mousepoint.y+randomYPos);
	        System.out.println(mousepoint.x+randomXPos+"   "+(mousepoint.y+randomYPos));   
			robot.delay(rest1);
			//robot.mouseMove(mousepoint.x,mousepoint.y);
		}
		timer.cancel();
	}
	/****************************************************************
	 * 默认的文件前缀为GuiCamera，文件格式为PNG格式 The default construct will use the default
	 * Image file surname "GuiCamera", and default image format "png"
	 ****************************************************************/
	public ControlUtils() {
		fileName = defaultName;
		imageFormat = defaultImageFormat;

	}

	/****************************************************************
	 * @param s
	 *            the surname of the snapshot file
	 * @param format
	 *            the format of the image file, it can be "jpg" or "png"
	 *            本构造支持JPG和PNG文件的存储
	 ****************************************************************/
	public ControlUtils(String s, String format) {

		fileName = s;
		imageFormat = format;
	}

	/****************************************************************
	 * 对屏幕进行拍照 snapShot the Gui once
	 ****************************************************************/
	public void snapShot() {

		try {
			// 拷贝屏幕到一个BufferedImage对象screenshot
			BufferedImage screenshot = (new Robot())
					.createScreenCapture(new Rectangle(0, 0,
							(int) d.getWidth(), (int) d.getHeight()));
			serialNum++;
			// 根据文件前缀变量和文件格式变量，自动生成文件名
			String name = fileName + String.valueOf(serialNum) + "."
					+ imageFormat;
			File f = new File(name);
			System.out.print("Save File " + name);
			// 将screenshot对象写入图像文件
			ImageIO.write(screenshot, imageFormat, f);
			System.out.print("..Finished!\n");
		} catch (Exception ex) {
			System.out.println(ex);
		}
	}// snapshot

	public static boolean isProcessRunging(String serviceName) throws Exception {
		Process p = Runtime.getRuntime().exec(TASKLIST);
		BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line;
		while ((line = reader.readLine()) != null) {
			if (line.contains(serviceName)) {
				//System.out.println(line+"---");
				return true;
			}
		}
		return false;
	}
	public static void FFLogoutScript() throws Exception {
		Runtime.getRuntime().exec(FFLogout );
	}

	public static void killFirefox() throws Exception {
		Runtime.getRuntime().exec(KILL );
	}
	public static void killJAVA() throws Exception {
		Runtime.getRuntime().exec("taskKill /F /IM java.exe" );
	}

	public static void startFirefox() throws Exception {
		Runtime.getRuntime().exec(START);
	}
}
